package lsp.decoder;

import java.io.IOException;

import lsp.base.LspParams;
import lsp.client.LspClient;

public class Requester {
	
	private LspClient lspClient;
	
	public Requester(String serverHost, int port) throws IOException {
		lspClient = new LspClient(serverHost, port, new LspParams(5000, 5));
	}

	public String request(String hash, int lower, int upper) throws PasswordNotFoundException {
		lspClient.write(String.format("C %s %d %d", hash, lower, upper).getBytes());
		
		String response = new String(lspClient.read());
		if (!response.startsWith("F")) {
			throw new PasswordNotFoundException(String.format("Password not found in interval %d..%d", lower, upper));
		}
		return response.substring(2);
	}
	
	public static class PasswordNotFoundException extends Exception {

		private static final long serialVersionUID = 1L;
		
		public PasswordNotFoundException(String message) {
			super(message);
		}
		
	}

	public short getConnId() {
		return lspClient.getConnId();
	}

}
